package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingOrderController extends BaseController {

    @Reference
   private ShippingOrderService shippingOrderService;

    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;

    @RequestMapping(value = "/list",name = "展示委托列表数据")
    public String findAll(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        //        查询委托单 应该有什么条件  companyId    按照生成时间排序 降序
        ShippingOrderExample example = new ShippingOrderExample();
        ShippingOrderExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getCompanyId());
        example.setOrderByClause("CREATE_TIME desc");
       PageInfo<ShippingOrder> pageInfo =  shippingOrderService.findAll(example,page,pageSize);
       request.setAttribute("page",pageInfo);

        return "cargo/entrust/entrust-list";
    }

    @RequestMapping(value = "/toAdd",name = "进入到添加委托列表页面")
    public String toAdd(String packingListId){
        Packing packing = packingService.findById(packingListId);
        String marks = packing.getMarks();
        ShippingOrder shippingOrder = new ShippingOrder();
        shippingOrder.setShippingOrderId(packingListId);
        shippingOrder.setShipper("传智播客");
        shippingOrder.setMarks(marks);
        String exportIds = packing.getExportIds();
        String[] split = exportIds.split(",");
        Export export = null;
                //拼接信用证号
        StringBuffer lcNo = new StringBuffer();
        for (int i = 0; i < split.length; i++) {
             export = exportService.findById(split[i]);
            String lcno = export.getLcno();
            if (i==split.length-1){
                lcNo.append(lcno);
            }else {
                lcNo.append(lcno).append(",");
            }
        }
       shippingOrder.setLcNo(lcNo.toString());
        export = exportService.findById(split[0]);
        String consignee = export.getConsignee();
        shippingOrder.setConsignee(consignee);
        shippingOrder.setNotifyParty(consignee);
        String shipmentPort = export.getShipmentPort();
        shippingOrder.setPortOfLoading(shipmentPort);
        String destinationPort = export.getDestinationPort();
        shippingOrder.setPortOfDischar(destinationPort);
        request.setAttribute("entrust",shippingOrder);
        return "cargo/entrust/entrust-add";
    }

    @RequestMapping(value = "/edit",name = "保存委托列表方法")
    public String edit(ShippingOrder shippingOrder){
            shippingOrder.setCompanyId(getCompanyId());
            shippingOrder.setCompanyName(getCompanyName());
            shippingOrder.setState(0);//0草稿 1已上报
           Packing packing = packingService.findById(shippingOrder.getShippingOrderId());
           packing.setState(1); //修改装箱状态为1 带代表已生成委托
            packingService.update(packing);
            shippingOrderService.save(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/delete",name = "删除委托列表方法")
    public String delete(String id){
        shippingOrderService.deleteById(id);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/downloadExcel",name = "生成委托书PDF文件的方法")
    public void downloadExcel(String id) throws Exception {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
//shipper  consignee  createTime  lcNo  notifyParty  loadingDate  limitDate   portOfLoading  portOfTrans
// portOfDischar isBatch isTrans  copyNum  specialCondition
        //2.有参数的PDF
        //1.读取到模板
        String templetPath = "G:\\LLZcode\\shipping.jasper";
        FileInputStream inputStream = new FileInputStream(templetPath);
        Map<String,Object> parameters = new HashMap();
        parameters.put("shipper",shippingOrder.getShipper());
        parameters.put("consignee",shippingOrder.getConsignee());
        parameters.put("createTime",shippingOrder.getCreateTime());
        parameters.put("lcNo",shippingOrder.getLcNo());
        parameters.put("notifyParty",shippingOrder.getNotifyParty());
        parameters.put("loadingDate",shippingOrder.getLoadingDate());
        parameters.put("limitDate",shippingOrder.getLimitDate());
        parameters.put("portOfLoading",shippingOrder.getPortOfLoading());
        parameters.put("portOfTrans",shippingOrder.getPortOfTrans());
        parameters.put("portOfDischar",shippingOrder.getPortOfDischar());
        parameters.put("isBatch",shippingOrder.getIsBatch());
        parameters.put("isTrans",shippingOrder.getIsTrans());
        parameters.put("copyNum",shippingOrder.getCopyNum());
        parameters.put("specialCondition",shippingOrder.getSpecialCondition());
        //即使是不需要参数填充也要有个空的map datasource是数据源 假如不需要可以放一个空数据源 也可以不写
        //填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream,parameters,new JREmptyDataSource());
        //导出pdf
        JasperExportManager.exportReportToPdfStream(jasperPrint,new FileOutputStream("d://shppingExport.pdf"));
    }

    @RequestMapping(value = "showMap", name = "显示路线")
    public String showMap(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shippingOrder", shippingOrder);
        return "cargo/entrust/map";
    }


}
